import { getLoginInfo, getUserList } from '@/api/getData'
const state = {
  loginInfo: {},
  list: [],
  queryParams: {
    page: 1,
    size: 10,
    total: 0
  }
}
const mutations = {
  SET_LOGIN_INFO (state, data) {
    state.loginInfo = Object.assign({}, data)
    window.localStorage.setItem('loginInfo', JSON.stringify(state.loginInfo))
  },
  SET_USER_LIST (state, data) {
    state.list = [].concat(data)
  },
  SET_QUERY_PARAMS (state, params) {
    state.queryParams = Object.assign(state.queryParams, params)
  }
}
const actions = {
  _get_login_info ({ commit }) {
    return new Promise((resolve, reject) => {
      getLoginInfo().then(res => {
        // eslint-disable-next-line no-unused-vars
        const { error, payload, message } = res
        if (error === 200) {
          commit('SET_LOGIN_INFO', payload)
          resolve(message)
        } else {
          reject(message)
        }
      })
    })
  },
  _get_user_list ({commit}, params) {
    return new Promise((resolve, reject) => {
      getUserList(params).then(res => {
        const { error, payload } = res
        if (error === 200) {
          const { list, page, total } = payload
          commit('SET_USER_LIST', list)
          commit('SET_QUERY_PARAMS', {total, page})
          resolve(payload)
        }
      })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
